package com.test.springai.config;

import com.alibaba.cloud.ai.memory.jdbc.MysqlChatMemoryRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 9/5/25 4:09 PM
 * @modifiedBy:
 * @version: 1.0
 */
@Configuration
public class MysqlMemoryConfig {

  @Value("${spring.ai.chat.memory.repository.jdbc.mysql.jdbc-url}")
  private String mysqlJdbcUrl;

  @Value("${spring.ai.chat.memory.repository.jdbc.mysql.username}")
  private String mysqlUsername;

  @Value("${spring.ai.chat.memory.repository.jdbc.mysql.password}")
  private String mysqlPassword;

  @Value("${spring.ai.chat.memory.repository.jdbc.mysql.driver-class-name}")
  private String mysqlDriverClassName;

  @Bean
  public MysqlChatMemoryRepository mysqlChatMemoryRepository() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(mysqlDriverClassName);
    dataSource.setUrl(mysqlJdbcUrl);
    dataSource.setUsername(mysqlUsername);
    dataSource.setPassword(mysqlPassword);
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    return MysqlChatMemoryRepository.mysqlBuilder().jdbcTemplate(jdbcTemplate).build();
  }
}
